<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="edu.ccut.saturn.component.SaturnData"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/extjs/include/all.jsp"%>
<title></title>
<script type="text/javascript">
	var userId = '${__SATURN_AUTORITY_USER__.id}';
	Ext.onReady(function(){
		Ext.QuickTips.init();
		var resultWidth = Ext.get("form-ct").getWidth();
		var userId = '${__SATURN_AUTORITY_USER__.id}';
		var states = [['2,3','全部'], ['2','代办'],['3','协办']];
        var simpleStore = new Ext.data.SimpleStore({
            fields: ['state','stateName']
        });
        simpleStore.loadData(states);
		var panel = new Ext.FormPanel({
		 	frame:true,
		 	layout:'form',
		 	renderTo:'form-ct',
		 	frame:true,
		 	labelWidth: 60,
		 	title:'查询代办任务',
		    items: [{
	            layout:'column',
	            items: [{
	            	columnWidth:.25,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '委托人',
		                    name: 'delegate_from_name',
		                    id: 'delegate_from_name',
		                    readOnly:true 
		                },{
		                    xtype:'hidden',
		                    name: 'delegate_from',
		                    id: 'delegate_from'
		                },{
			                columnWidth:.5,
			                layout: 'form',
			                items: [{
				                    xtype:'combo',
				                    fieldLabel: '代办方式',
				                    name:'delegate_type',
				                    hiddenName:'bizstate',
				                    store: simpleStore,
				                    displayField: 'stateName',
				                    valueField: 'state',
				                    value:'2,3',
				                    mode: 'local',
								    triggerAction: 'all',
								    width:130,
								    editable: false,
		 							typeAhead: true
				                }]
	            		}]
		            },{
	                columnWidth:.2,
	                layout: 'form',
	                items: [{
		                    xtype:'button',
		                    text: '选择委托人',
		                    handler:chooseDelegateFrom
		                }]
	            },{
	            	columnWidth:.25,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '代办人',
		                    name: 'delegate_to_name',
		                    id: 'delegate_to_name',
		                    readOnly:true
		                },{
		                    xtype:'hidden',
		                    name: 'delegate_to',
		                    id: 'delegate_to'
		                },{
		                    xtype:'hidden',
		                    name: 'delegate_to_type',
		                    id: 'delegate_to_type'
		                }]
		        },{
	                columnWidth:.2,
	                layout: 'form',
	                items: [{
		                    xtype:'button',
		                    text: '选择协办人',
		                    handler:chooseDelegateTo
		                }]
	            }]
       	    }],

	        buttons: [{
	            text: '查询',
	            handler:function(){
		            var delegate_from = Ext.get("delegate_from").dom.value;
		            var delegate_to = Ext.get("delegate_to").dom.value;
		            var delegate_to_type = Ext.get("delegate_to_type").dom.value;

		            if(delegate_from==delegate_to&&delegate_to_type=='user'){
		            	Ext.Msg.alert('信息',"委托人不能与代办人相同",function(){
							 return false;
						}); 
		            }else{
	            		store.load({params: {delegate_from:Ext.get("delegate_from").dom.value,
									delegate_to:Ext.get("delegate_to").dom.value}});
					}
	            }
	        },{
	            text: '重置',
	            handler:function(){
	            	panel.getForm().reset();
	            }
	        }]
	    });
	    
	   var store = new Ext.data.JsonStore({
		    url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.manager::/delegateModule/action/QueryDelegateTask.action',
		    root: 'result',
		    totalProperty:'count',
		    fields: [
	               {name: 'workitem_name', mapping: 'workitem_name'},
	               {name: 'process_name', mapping: 'process_name'},
	               {name: 'process_instance_name', mapping: 'process_instance_name'},
		    	   {name: 'workitem_state', mapping: 'workitem_state'},
		    	   {name: 'workitem_start_time', mapping: 'workitem_start_time'},
	               {name: 'act_inst_limit_number', mapping: 'start_time'}
	           ]
		});
		
	  
	    var grid = new Ext.grid.GridPanel({
       	    store: store,
	        columns: [
	       	    {header: "工作项名称",width:resultWidth*0.15,dataIndex: 'workitem_name', sortable: true},
	            {header: "业务流程名称",width:resultWidth*0.15, dataIndex: 'process_name', sortable: true},
	            {header: "流程实例名称",width:resultWidth*0.15, dataIndex: 'process_instance_name', sortable: true},
	            {header: "当前状态", width:resultWidth*0.1,dataIndex: 'workitem_state', sortable: true,renderer:showState},
	            {header: "启动时间", width:resultWidth*0.15,dataIndex: 'workitem_start_time', sortable: true},
	            {header: "时间限制", width:resultWidth*0.15,dataIndex: 'act_inst_limit_number', sortable: true}
	        ],
	        renderTo:'task-grid',
	        height:400,
	        frame:true,
	        bodyStyle:'width:100%',
	        loadMask: {msg:'正在加载数据，请稍侯……'}, 
	        bbar: new Ext.PagingToolbar({ 
		            pageSize: 20, 
		            store: store, 
		            displayInfo: true, 
		            displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录',
		            emptyMsg: "没有数据显示" 
		    }) 
    });
   	store.load({params: {start:0,limit:20,
			   	delegate_from:Ext.get("delegate_from").dom.value,
			   	delegate_to:Ext.get("delegate_to").dom.value,
			   	bizstate:Ext.get("bizstate").dom.value}});
	store.on('beforeload',function(){
	    Ext.apply(this.baseParams,{bizstate:Ext.get("bizstate").dom.value});
    });
    

    function chooseDelegateFrom(){
    	var root=new Ext.tree.AsyncTreeNode({
		id:"root_participant",
		text:"参与者"});
		var tree=new Ext.tree.TreePanel({
			root:root,
			loader: new Ext.tree.TreeLoader({url:'${pageContext.request.contextPath}/edu.ccut.saturn.workflow.manager::/agentManagerModule/action/GetTreeJson.action'}),
			width:400,
			autoScroll: true
		});
	
  		var window = new Ext.Window({
			title: '添加委托人',
			width: 400,
			height:300,
			minWidth: 300,
			minHeight: 200,
			layout: 'fit',
			plain:true,
			items:tree,
			buttonAlign:'center',
			modal:true
		});
		  tree.on('click', function(node){
					addDelegateFrom(node.id,node.text);
				},this);
			window.show();
		}
		
	function chooseDelegateTo(){
		 	var root=new Ext.tree.AsyncTreeNode({
				id:"root_participant",
				text:"参与者"});
			var tree=new Ext.tree.TreePanel({
				root:root,
				loader: new Ext.tree.TreeLoader({url:'${pageContext.request.contextPath}/edu.ccut.saturn.workflow.manager::/agentManagerModule/action/GetTreeJson.action'}),
				width:400,
				autoScroll: true
			});
			
		  		var window = new Ext.Window({
					title: '添加代办人',
					width: 400,
					height:300,
					minWidth: 300,
					minHeight: 200,
					layout: 'fit',
					plain:true,
					items:tree,
					buttonAlign:'center',
					modal:true
				});
		  	tree.on('click', function(node){
					addDelegateTo(node.id,node.text);
				},this);
			window.show();
		}
		
	function addDelegateFrom(userid,name){
		 if(userid=='root_participant'){
				return false;
		 }
		 if(userid!='rootDept'&& userid!='rootRole'){
		 	var type = userid.split('_')[0];
		 	var id = userid.split('_')[1];
		 	if(type=='deptUser'||type=='roleUser'){
		 		Ext.get("delegate_from").dom.value =id;
		 		Ext.get("delegate_from_name").dom.value =name;
		 	}
		 }
	 }
	
	function addDelegateTo(userid,name){
		 if(userid=='root_participant'){
				return false;
		 }
		 if(userid!='rootDept'&& userid!='rootRole'){
		 	var type = userid.split('_')[0];
		 	var id = userid.split('_')[1];
		 	if(type=='deptUser'||type=='roleUser'){
		 		Ext.get("delegate_to").dom.value =id;
		 		Ext.get("delegate_to_name").dom.value =name;
		 		Ext.get("delegate_to_type").dom.value ="user";
		 	}
		 	else if(type=='dept'){
		 		Ext.get("delegate_to_type").dom.value ="dept";	
		 		Ext.get("delegate_to").dom.value =id;
		 		Ext.get("delegate_to_name").dom.value =name; 		
		 	}
		 	else if(type=='role'){
		 		Ext.get("delegate_to_type").dom.value ="role";
		 		Ext.get("delegate_to").dom.value =id;
		 		Ext.get("delegate_to_name").dom.value =name;
		 	}
		 }
	 }
	 
	  function showState(value){
	 	if(value=='10'){
	 		return '运行';
	 	}
	 	if(value=='12'){
	 		return '完成';
	 	}
	 }
});
</script>
<div id='form-ct' width='100%'></div>
<div id='task-grid' width='100%'></div>
</head>
</html>